using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_EditProviderProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("providersecurelogin.aspx");
            }
        }
        else
        {
            Response.Redirect("providersecurelogin.aspx");
        }

        if (!IsPostBack)
        {
            ShowProfile();
        }
    }

    private void ShowProfile()
    {
        Profile _userprofile = new Profile();
        ProfileDB _userprofiledb = new ProfileDB();
        Contact _contact = new Contact();
        ContactDB _contactdb = new ContactDB();

        _userprofile = _userprofiledb.GetProfile(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));

        if (_userprofile != null)
        {
            UsrSalutation1.Saluation = _userprofile.Salutation;
            txtFirstName.Text = _userprofile.FirstName;
            txtSurName.Text = _userprofile.SurName;
            UsrDate1.Day = _userprofile.DOB.Day;
            UsrDate1.Month = _userprofile.DOB.Month;
            UsrDate1.Year = _userprofile.DOB.Year;
            UsrGender1.Gender = _userprofile.Gender.ToString();
            ddlMaritalStatus.SelectedValue = _userprofile.MaritalStatus.ToString();
            ddlOccupationType.SelectedValue = _userprofile.OccupationType.ToString();
            ddlOccupation.SelectedValue = _userprofile.Occupation.ToString();

            UsrIdType1.IdType = _userprofile.IDType;
            txtIDNo.Text = _userprofile.IDNo;
            txtHighEducation.Text = _userprofile.HighEducation;
            UsrNationality1.Nationality = _userprofile.Nationality;


            _contact = _contactdb.GetContact(_userprofile.RefCEUserId);
            if (_contact != null)
            {
                txtMobileNo.Text = _contact.Mobile;
                txtHomePhoneNo.Text = _contact.HomePhone;
                txtOfficeNo.Text = _contact.OfficePhone;
                txtOfficeFaxNo.Text = _contact.OfficeFax;
                txtOfficeUnitNo1.Text = _contact.OfficeUnitNo;
                txtOfficeUnitNo2.Text = _contact.OfficeLevelNo;
                txtOfficeStreet.Text = _contact.OfficeStreet;
                txtOfficeCity.Text = _contact.OfficeCity ;
                ddlOfficeCountry.Text = _contact.OfficeCountry;
                txtOfficePostCode.Text = _contact.OfficePostCode;
            }
            else
            {
                lblMessage.Text = "No contact found.";
            }
        }
    }
    protected void btnUpdateProfile_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Profile _userprofile = new Profile();
            ProfileDB _userprofiledb = new ProfileDB();
            Contact _contact = new Contact();
            ContactDB _contactdb = new ContactDB();

            long _profileresult = -1;
            long _contactresult = -1;

            _userprofile = _userprofiledb.GetProfile(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));

            if (_userprofile != null)
            {
                _userprofile.Salutation = UsrSalutation1.GetSalutation();
                _userprofile.FirstName = txtFirstName.Text;
                _userprofile.SurName = txtSurName.Text;
                _userprofile.DOB = UsrDate1.GetDate();
                _userprofile.Gender = Convert.ToChar(UsrGender1.GetGender());
                _userprofile.MaritalStatus = Convert.ToInt32(ddlMaritalStatus.SelectedValue);
                _userprofile.OccupationType = Convert.ToInt32(ddlOccupationType.SelectedValue);
                _userprofile.Occupation = Convert.ToInt32(ddlOccupation.SelectedValue);

                _userprofile.IDType = UsrIdType1.GetIDType();
                _userprofile.IDNo = txtIDNo.Text;
                _userprofile.HighEducation = txtHighEducation.Text;
                _userprofile.Nationality = UsrNationality1.GetNationality();
                //_userprofile.RefCEUserId = _useraccount.CEID;
                _userprofile.Status = "UPDATE";
                _profileresult = _userprofiledb.Insert(_userprofile);

                if (_profileresult > 0)
                {
                    _contact = _contactdb.GetContact(_userprofile.RefCEUserId);
                    if (_contact != null)
                    {
                        _contact.Mobile = txtMobileNo.Text;
                        //_contact.Email = _useraccount.Email;
                        _contact.HomePhone = txtHomePhoneNo.Text;
                        //_contact.HomeFax = txtHomeFaxNo.Text;
                        //_contact.HomeUnitNo1 = txtHomeUnitNo1.Text;
                        //_contact.HomeUnitNo2 = txtHomeUnitNo2.Text;
                        //_contact.HomeStreet = txtHomeStreet.Text;
                        //_contact.HomeCity = txtHomeCity.Text;
                        //_contact.HomeCountry = ddlHomeCountry.Text;
                        //_contact.HomePostCode = txtHomePostCode.Text;

                        _contact.OfficePhone = txtOfficeNo.Text;
                        _contact.OfficeFax = txtOfficeFaxNo.Text;
                        _contact.OfficeUnitNo = txtOfficeUnitNo1.Text;
                        _contact.OfficeLevelNo = txtOfficeUnitNo2.Text;
                        //_contact.OfficeUnitNo2 = txtOfficeUnitNo2.Text;
                        _contact.OfficeStreet = txtOfficeStreet.Text;
                        _contact.OfficeCity = txtOfficeCity.Text;
                        _contact.OfficeCountry = ddlOfficeCountry.Text;
                        _contact.OfficePostCode = txtOfficePostCode.Text;
                        _contact.Status = true;
                        //_contact.RefCEUserId = _useraccount.CEID;
                        _contactresult = _contactdb.Insert(_contact);
                    }
                    else
                    {
                        lblMessage.Text = "No contact found to update!<br />";
                    }
                }

                if (_profileresult > 0 && _contactresult > 0)
                {
                    lblMessage.Text = "Updated profile successfully.";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    lblMessage.Text = "Updating profile failed.";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }

            }
        }
    }
    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Ready";
        lblMessage.ForeColor = System.Drawing.Color.Blue;
        //txtEmailAddress.Text = "";
        //txtAuthCode.Text = "";
        //txtAuthCodeConfirm.Text = "";
        txtFirstName.Text = "";
        txtSurName.Text = "";
        txtIDNo.Text = "";
        txtHighEducation.Text = "";
        txtHomePhoneNo.Text = "";
        txtMobileNo.Text = "";
        txtOfficeNo.Text = "";
        txtOfficeFaxNo.Text = "";
        txtOfficePostCode.Text = "";
        txtOfficeStreet.Text = "";
        txtOfficeCity.Text = "";
        txtOfficeUnitNo1.Text = "";
        txtOfficeUnitNo2.Text = "";
        ddlOfficeCountry.SelectedIndex = 0;
    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("adminhome.aspx");
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("assignprovidertype.aspx");
    }
}